import junit.framework.*;
import java.util.*;
import java.sql.*;


public class DomainTest extends TestCase{

  protected void setUp() throws Exception{
    super.setUp();
  }

  // This method tests the user class
  public void testUser(){
    Name testName = new Name("Dr.", "Bob", "Lob", "Law");
    User testUser = new User("ExampleUser", "password", testName, 1);
    PasswordHash passwordHelper = new PasswordHash();
    assertEquals("ExampleUser", testUser.getUsername());
    assert(testUser.hasPasswordHash(passwordHelper.encrypt("password")));
    assertEquals(testName, testUser.getName());
    assertEquals(1, testUser.getAccessLevel());
    assert(!testUser.isSuspended());

    testUser.setUsername("anotherUsername");
    assertEquals("anotherUsername", testUser.getUsername());
    Name anotherName = new Name("Mr.", "Lary", "the", "Cable Guy");
    testUser.setName(anotherName);
    assertEquals(anotherName, testUser.getName());
    testUser.setAccessLevel(3);
    assertEquals(3, testUser.getAccessLevel());
    testUser.setSuspendState(true);
    assert(testUser.isSuspended());
  }

  // this method tests the patient info class
  public void testPatientInfo(){
    Patient testPatient = new Patient();
    String [] testAddress = new String[4];
    testAddress[0] = "North Ave";
    testAddress[1] = "Atlanta";
    testAddress[2] = "Georgia";
    testAddress[3] = "30332";
    long testNumber = 8880001111l;
    PatientInfo testInfo = new PatientInfo(testPatient, testAddress, testNumber, "user@example.com", "male", 18, "CVS", "Wells Fargo", "Peanuts", null);
    assertEquals(testPatient, testInfo.getPatient());
    assertEquals(testAddress[0], testInfo.getAddress().getStreet());
    assertEquals(testAddress[1], testInfo.getAddress().getCity());
    assertEquals(testAddress[2], testInfo.getAddress().getState());
    assertEquals(testAddress[3], Integer.toString(testInfo.getAddress().getZipCode()));
    assertEquals(testNumber, testInfo.getPhoneNumber());
    assert("user@example.com".equals(testInfo.getEmail()));
    assert("male".equals(testInfo.getGender()));
    assertEquals(18, testInfo.getAge());
    assert("CVS".equals(testInfo.getPharmacy()));
    assert("Wells Fargo".equals(testInfo.getInsuranceCarrier()));
    assert("Peanuts".equals(testInfo.getAllergies()));
    
    long newNumber = 1112223333l;
    testInfo.setPhoneNumber(newNumber);
    assertEquals(newNumber, testInfo.getPhoneNumber());
    testInfo.setEmail("another@email.com");
    assert("another@email.com".equals(testInfo.getEmail()));
    testInfo.setGender("female");
    assert("female".equals(testInfo.getGender()));
    testInfo.setAge(20);
    assertEquals(20, testInfo.getAge());
    testInfo.setPharmacy("Walgreens");
    assert("Walgreens".equals(testInfo.getPharmacy()));
    testInfo.setInsuranceCarrier("Bank of America");
    assert("Bank of America".equals(testInfo.getInsuranceCarrier()));
    testInfo.setAllergies("Peanuts, Dust");
    assert("Peanuts, Dust".equals(testInfo.getAllergies()));
  }

  //this class tests the address class
  public void testAddress(){
    String testStreet = "North Avenue";
    String testCity = "Atlanta";
    String testState = "Georgia";
    int testZipCode = 30332;
    Address testAddress = new Address(testStreet, testCity, testState, testZipCode);
    assert(testStreet.equals(testAddress.getStreet()));
    assert(testCity.equals(testAddress.getCity()));
    assert(testState.equals(testAddress.getState()));
    assertEquals(testZipCode, testAddress.getZipCode());

    String newStreet = "Piedmont";
    String newCity = "Orlando";
    String newState = "Florida";
    int newZip = 12345;
    testAddress.setStreet(newStreet);
    assert(newStreet.equals(testAddress.getStreet()));
    testAddress.setCity(newCity);
    assert(newCity.equals(testAddress.getCity()));
    testAddress.setState(newState);
    assert(newState.equals(testAddress.getState()));
    testAddress.setZipCode(newZip);
    assertEquals(newZip, testAddress.getZipCode());
  }
/**
  public void testAppointment(){
    TreatmentRecord testRecord = new TreatmentRecord(null);
    Appointment testAppointment = new Appointment(testRecord);
    Name testName = new Name("Dr.", "Bob", "Lob", "Law");
    Name nurseName = new Name("Mrs.", "April", "Lob", "Law");
    Doctor testDoctor = new Doctor("username", "password", testName, 3);
    Nurse testNurse = new Nurse("another", "pass", nurseName, 2); 

    assertEquals(testRecord, testAppointment.getTreatmentRecord());
    assertNull(testAppointment.getDesiredDoctor());
    assertNull(testAppointment.getAttendingNurse());
    assertNull(testAppointment.getReason());

    testAppointment.setDesiredDoctor(testDoctor);
    assertEquals(testDoctor, testAppointment.getDesiredDoctor());
    testAppointment.setAttendingNurse(testNurse);
    assertEquals(testNurse, testAppointment.getAttendingNurse());
    String testReason = "Yearly Physical";
    testAppointment.setReason(testReason);
    assertEquals(testReason, testAppointment.getReason());
  }
**/
  public void testBilling(){
    Name testName = new Name("Mr.", "Bob", "Lob", "Law");
    Patient testPatient = new Patient("username", "password", testName, 1);
    Billing testBilling = new Billing(testPatient);

    assertEquals(testPatient, testBilling.getPatient());
    assertTrue(testBilling.getBalance() == 0);

    Name anotherName = new Name("Mr.", "Com", "Pu", "Ter");
    Patient anotherPatient = new Patient("another", "pass", anotherName, 1);
    testBilling.setPatient(anotherPatient);
    assertEquals(anotherPatient, testBilling.getPatient());

    testBilling.setBalance(2000);
    assertEquals(2000, testBilling.getBalance());
  }

  public void testDoctorsOrders(){

    DoctorsOrders testOrders = new DoctorsOrders();
    assertNull(testOrders.getTreatmentRecord());
    assertNull(testOrders.getValue());
    TreatmentRecord testRecord = new TreatmentRecord(null);
    String testValue = "Eat an apple every day";
    testOrders.setValue(testValue);
    assertEquals(testValue, testOrders.getValue());
  }

  public void testName(){
    String testTitle = "Mr.";
    String testFirst = "Bob";
    String testMiddle = "Lob";
    String testLast = "Law";
    Name testName = new Name(testTitle, testFirst, testMiddle, testLast);
    assertEquals(testTitle, testName.getTitle());
    assertEquals(testFirst, testName.getFirstName());
    assertEquals(testMiddle, testName.getMiddleName());
    assertEquals(testLast, testName.getLastName());

    String anotherTitle = "Mrs.";
    String anotherFirst = "Larry";
    String anotherMiddle = "Von";
    String anotherLast = "Schroder";

    testName.setTitle(anotherTitle);
    assertEquals(anotherTitle, testName.getTitle());
    testName.setFirstName(anotherFirst);
    assertEquals(anotherFirst, testName.getFirstName());
    testName.setMiddleName(anotherMiddle);
    assertEquals(anotherMiddle, testName.getMiddleName());
    testName.setLastName(anotherLast);
    assertEquals(anotherLast, testName.getLastName());
  }

  public void testTreatmentRecord(){
    Name testName = new Name("Mr.", "Bob", "Lob", "Law");
    Patient testPatient = new Patient("username", "password", testName, 1);
    String[] testAddress = new String[4];
    testAddress[0] = "North Ave";
    testAddress[1] = "Atlanta";
    testAddress[2] = "Georgia";
    testAddress[3] = "30332";
    PatientInfo testInfo = new PatientInfo(testPatient, testAddress, 1112223333, "user@example.com", "male", 20, "CVS", "Wells Fargo", "Peanuts", null);
    TreatmentRecord testRecord = new TreatmentRecord(testInfo);
    assertEquals(testInfo, testRecord.getPatientInfo());
    assertNull(testRecord.getAppointment());
    assertNull(testRecord.getVitalSigns());
    assertNull(testRecord.getDiagnosis());
    assertNull(testRecord.getDoctorsOrders());
  /**
    Appointment testAppointment = new Appointment(testRecord);
    testRecord.setAppointment(testAppointment);
    assertEquals(testAppointment, testRecord.getAppointment());

    VitalSigns testSigns = new VitalSigns(testRecord);
    testRecord.setVitalSigns(testSigns);
    assertEquals(testSigns, testRecord.getVitalSigns());
  **/
    String testDiagnosis = "Chicken pox";
    testRecord.setDiagnosis(testDiagnosis);
    assertEquals(testDiagnosis, testRecord.getDiagnosis());

    DoctorsOrders testOrders = new DoctorsOrders();
    testRecord.setDoctorsOrders(testOrders);
    assertEquals(testOrders, testRecord.getDoctorsOrders());
  }

  public void testVitalSigns(){
    Name testName = new Name("Mr.", "Bob", "Lob", "Law");
    Patient testPatient = new Patient("username", "password", testName, 1);
    String[] testAddress = new String[4];
    testAddress[0] = "North Ave";
    testAddress[1] = "Atlanta";
    testAddress[2] = "Georgia";
    testAddress[3] = "30332";
    PatientInfo testInfo = new PatientInfo(testPatient, testAddress, 1112223333, "user@example.com", "male", 20, "CVS", "Wells Fargo", "Peanuts", null);
    TreatmentRecord testRecord = new TreatmentRecord(testInfo);
    VitalSigns testSigns = new VitalSigns(-1);
    assert(testSigns.getBloodPressure() == 0);
    assert(testSigns.getPulse() == 0);
    assert(testSigns.getTemperature() == 0);
    assert(testSigns.getHeight() == 0);
    assert(testSigns.getWeight() == 0);

    testSigns.setBloodPressure(120);
    assertEquals(120, testSigns.getBloodPressure());
    testSigns.setPulse(60);
    assertEquals(60, testSigns.getPulse());
    testSigns.setTemperature(98);
    assertEquals(98, testSigns.getTemperature());
    testSigns.setHeight(72);
    assertEquals(72, testSigns.getHeight());
    testSigns.setWeight(180);
    assertEquals(180, testSigns.getWeight());
  }

  public void testAppointmentCRUD(){
    Calendar now = Calendar.getInstance();
    long timeMils = now.getTimeInMillis();
    java.sql.Date date = new java.sql.Date(timeMils);
    java.sql.Time time = new Time(timeMils);
    // Create
    boolean created = Appointment.createAppointment("username", date, time);
    assertTrue(created);
    /**
    // Read
    Appointment appt = Appointment.readAppointment(1);
    
    // Update
    long newTimeMils = timeMils + 100000;
    Time newTime = new Time(newTimeMils);
    boolean updated = Appointment.updateAppointment("username", date, newTime);
    assertTrue(updated);
    // Delete
    boolean deleted = Appointment.deleteAppointment("username", date, newTime);
    assertTrue(deleted);
    **/
  }

  public void testUserCRUD(){
    // Patient CRUD
    // Create
    boolean created = Patient.createPatient("username", "encrypted_password", false);
    assertTrue(created);
    // Read

    // Update
    boolean updated = Patient.updatePatient("username", "another_password", true);
    assertTrue(updated);
    // Delete
    boolean deleted = Patient.deletePatient("username");
    assertTrue(deleted);
  }

  public void testTreatmentRecordCrud(){
    // Create
    boolean created = TreatmentRecord.createTreatmentRecord("username", 1, "diagnosis");
    assertTrue(created);
    // Read

    // Update
    boolean updated = TreatmentRecord.updateTreatmentRecord("username", 1, "another diagnosis");
    assertTrue(updated);
    // Delete
    boolean deleted = TreatmentRecord.deleteTreatmentRecord("username", 1);
    assertTrue(deleted);

  }

  public void testDoctorsOrdersCRUD(){
    //Create
    boolean created = DoctorsOrders.createDoctorsOrders(1, "Apple a day");
    assertTrue(created);
    // Read

    // Update
    boolean updated = DoctorsOrders.updateDoctorsOrders(1, "Plenty of carrots");
    assertTrue(updated);
    // Delete
    boolean deleted = DoctorsOrders.deleteDoctorsOrders(1);
    assertTrue(deleted);
  }

  public void nameCRUD(){
  boolean created  = Name.createName("burdell", "Mr.", "George", "p", "burdell");
  assert(created);

  Name another = new Name("Mr.", "George", "p", "burdell");
  Name name = Name.readName(another);

  boolean updated  = Name.updateName("burdell", "Mrs.", "april", "f", "burdell");
  assert(updated);

  boolean deleted = Name.deleteName("burdell");
  assert(deleted);
  }
}
